#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
typedef unsigned long long ull;

#define mo 23

const int N = 1e6 + 5, mod = 1e9 + 7, hashmod = 20040701;

std::vector<ull> h[hashmod + 5];

int n, m, a, b, mx;
int e_cnt, heads[N];

struct Edge {
  int v, nxt, w;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

int flag[N], head[N];
ull po[N], f[N];
int tmp;

int adt(int a, int b) {
  if (a + b >= mod) return a + b - mod;
  return a + b;
}

void dfs(int u, int fa) {
  for (int i = heads[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa) f[v] = f[u] ^ po[e[i].w], dfs(v, u);
}

inline void insert(ull a) {
  int b = a % hashmod;
  h[b].push_back(a);
}

inline bool find(ull a) {
  int b = a % hashmod;
  for (auto &j : h[b])
    if (j == a) return true;
  return false;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("tree.in", "r", stdin);
  freopen("tree.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1, u, v, w; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    w++;
    mx = std::max(mx, w);
    add(u, v, w);
    add(v, u, w);
  }
  po[0] = 1;
  for (int i = 1; i <= mx; i++) po[i] = (ull)po[i - 1] * mo;
  dfs(1, 0);
  insert(0);
  for (int i = 1; i <= mx; i++) insert(po[i]);
  scanf("%d%d", &a, &b);
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    if (find(f[a % n + 1] ^ f[b % n + 1])) ans++;
    a = 1ll * a * 666073 % 1000000007;
    b = 1ll * b * 233 % 998244353;
  }
  printf("%d", ans);
  return 0;
}
